//
//  ByApiOrder.m
//  oc_knowledge
//
//  Created by mc on 17/1/16.
//  Copyright © 2017年 com.itboye. All rights reserved.
//

#import "ByApiOrder.h"
#import "ByModelOrder.h"
#import "ByModelOrderPay.h"
@implementation ByApiOrder
#pragma mark - 请求接口
/** 获取订单列表*/
+ (void)getOrderList:(ByNetParamsModel *)model
             success:(void(^)(ByNetBaseResult *result))success
             failure:(void(^)(ByNetBaseResult *result))failure {
    
    static NSString *apiType = @"By_Order_query";
    static NSInteger apiVersion = 101;
    int rand = arc4random() % 1000;
    
    NSString *requestId = [NSString stringWithFormat:@"%ld%d", (NSInteger)[NSDate currentDateTimeInterval], rand];
    
    // 拼接参数
    NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
    [params setObject:model.uid forKey:@"uid"];
    if (model.page_size) {
        [params setObject:model.page_size forKey:@"page_size"];
    }
    if (model.page_index) {
        [params setObject:model.page_index forKey:@"page_index"];
    }
    if (model.query_status) {
        [params setObject:model.query_status forKey:@"query_status"];
    }
    if (model.s_id) {
        [params setObject:model.s_id forKey:@"s_id"];
    }
    
    [self post:requestId
       apiType:apiType
      dataType:ByNetDataTypeDictionary
     apiVerson:apiVersion
        params:params
       success:^(NSURLSessionDataTask *operation, id responseObject, NSString *notifyID) {
           
           ByNetBaseResult *result = [[ByNetBaseResult alloc] init];
           result.code = 0;
           result.msg = @"获取成功";
           ByModelOrder *model = [[ByModelOrder alloc] initContentWithDic:responseObject];
           result.data = model;
           result.notify_id = notifyID;
           
           if(success) {
               success(result);
           }
           
       } failure:^(NSURLSessionDataTask *operation, NSError *error){
           
           DLog(@"******** 获取订单列表 -> 失败: %@", error);
           
           ByNetBaseResult *result = [[ByNetBaseResult alloc] init];
           result.code = error.code;
           result.msg = error.domain;
           result.data = error.localizedDescription;
           if(failure) {
               failure(result);
           } else {
               [ByCoreToolAlert showFailureWithText:result.msg];
           }
       }];
}

/** 订单重支付接口*/
+ (void)repayOrder:(ByNetParamsModel *)model
           success:(void(^)(ByNetBaseResult *result))success
           failure:(void(^)(ByNetBaseResult *result))failure{
    static NSString *apiType = @"By_Order_repay";
    static NSInteger apiVersion = 101;
    int rand = arc4random() % 1000;
    
    NSString *requestId = [NSString stringWithFormat:@"%ld%d", (NSInteger)[NSDate currentDateTimeInterval], rand];
    
    // 拼接参数
    NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
    [params setObject:model.uid forKey:@"uid"];
    [params setObject:model.order_code forKey:@"order_code"];
    if (model.s_id) {
        [params setObject:model.s_id forKey:@"s_id"];
    }
    [self post:requestId
       apiType:apiType
      dataType:ByNetDataTypeDictionary
     apiVerson:apiVersion
        params:params
       success:^(NSURLSessionDataTask *operation, id responseObject, NSString *notifyID) {
           
           ByNetBaseResult *result = [[ByNetBaseResult alloc] init];
           result.code = 0;
           result.msg = @"获取成功";
           ByModelOrderPay *model = [[ByModelOrderPay alloc] initContentWithDic:responseObject];
           result.data = model;
           result.notify_id = notifyID;
           
           if(success) {
               success(result);
           }
           
       } failure:^(NSURLSessionDataTask *operation, NSError *error){
           
           DLog(@"******** 获取 -> 失败: %@", error);
           
           ByNetBaseResult *result = [[ByNetBaseResult alloc] init];
           result.code = error.code;
           result.msg = error.domain;
           result.data = error.localizedDescription;
           if(failure) {
               failure(result);
           } else {
               [ByCoreToolAlert showFailureWithText:result.msg];
           }
       }];

}
#pragma mark - 上传接口
/** 确认订单（确认收货）*/
+ (void)confirmOrder:(ByNetParamsModel *)model
             success:(void(^)(ByNetBaseResult *result))success
             failure:(void(^)(ByNetBaseResult *result))failure{
    static NSString *apiType = @"By_Order_receiveGoods";
    static NSInteger apiVersion = 101;
    int rand = arc4random() % 1000;
    
    NSString *requestId = [NSString stringWithFormat:@"%ld%d", (NSInteger)[NSDate currentDateTimeInterval], rand];
    
    // 拼接参数
    NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
    [params setObject:model.uid forKey:@"uid"];
    [params setObject:model.order_code forKey:@"order_code"];
    if (model.s_id) {
        [params setObject:model.s_id forKey:@"s_id"];
    }
    
    [self post:requestId
       apiType:apiType
      dataType:ByNetDataTypeString
     apiVerson:apiVersion
        params:params
       success:^(NSURLSessionDataTask *operation, id responseObject, NSString *notifyID) {
           
           ByNetBaseResult *result = [[ByNetBaseResult alloc] init];
           result.code = 0;
           result.msg = responseObject;
           result.data = responseObject;
           result.notify_id = notifyID;
           
           if(success) {
               success(result);
           }
           
       } failure:^(NSURLSessionDataTask *operation, NSError *error){
           
           DLog(@"******** 确认收货 -> 失败: %@", error);
           
           ByNetBaseResult *result = [[ByNetBaseResult alloc] init];
           result.code = error.code;
           result.msg = error.domain;
           result.data = error.localizedDescription;
           if(failure) {
               failure(result);
           } else {
               [ByCoreToolAlert showFailureWithText:result.msg];
           }
       }];

}

/** 取消订单*/
+ (void)cancleOrder:(ByNetParamsModel *)model
            success:(void(^)(ByNetBaseResult *result))success
            failure:(void(^)(ByNetBaseResult *result))failure{
    static NSString *apiType = @"By_Order_cancel";
    static NSInteger apiVersion = 101;
    int rand = arc4random() % 1000;
    
    NSString *requestId = [NSString stringWithFormat:@"%ld%d", (NSInteger)[NSDate currentDateTimeInterval], rand];
    
    // 拼接参数
    NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
    [params setObject:model.uid forKey:@"uid"];
    [params setObject:model.order_code forKey:@"order_code"];
    if (model.s_id) {
        [params setObject:model.s_id forKey:@"s_id"];
    }
    
    [self post:requestId
       apiType:apiType
      dataType:ByNetDataTypeString
     apiVerson:apiVersion
        params:params
       success:^(NSURLSessionDataTask *operation, id responseObject, NSString *notifyID) {
           
           ByNetBaseResult *result = [[ByNetBaseResult alloc] init];
           result.code = 0;
           result.msg = responseObject;
           result.data = responseObject;
           result.notify_id = notifyID;
           
           if(success) {
               success(result);
           }
           
       } failure:^(NSURLSessionDataTask *operation, NSError *error){
           
           DLog(@"******** 取消订单 -> 失败: %@", error);
           
           ByNetBaseResult *result = [[ByNetBaseResult alloc] init];
           result.code = error.code;
           result.msg = error.domain;
           result.data = error.localizedDescription;
           if(failure) {
               failure(result);
           } else {
               [ByCoreToolAlert showFailureWithText:result.msg];
           }
       }];

}
@end
